package com.example.listgame;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.listgame.fragment.RankingActivity;

public class CheckLevelActivity extends AppCompatActivity {

    private Button mSimple,mMedium,mDifficult,mBtnFragment,mBtnMusic,mBtn1;
    String username;
    MediaPlayer mediaPlayer;
    static Boolean isPlay = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_level);
        Bundle bundle = getIntent().getExtras();
        username = bundle.getString("username");
        mSimple = findViewById(R.id.simple);
        mMedium = findViewById(R.id.medium);
        mDifficult = findViewById(R.id.difficult);
        mBtnFragment = findViewById(R.id.btn_fragment);
        mBtnMusic = findViewById(R.id.btn_music);
        mBtn1 = findViewById(R.id.btn_1);
        PlayMusic();
        Onclick onclick = new Onclick();
        mSimple.setOnClickListener(onclick);
        mMedium.setOnClickListener(onclick);
        mDifficult.setOnClickListener(onclick);
        mBtnFragment.setOnClickListener(onclick);
        mBtn1.setOnClickListener(onclick);
    }

    public void PlayMusic() {
        mediaPlayer = MediaPlayer.create(this,R.raw.game_music);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        mBtnMusic.setBackgroundResource(R.drawable.music_on);
    }

    public void OnMusic(View v){
        if (isPlay == true){
            if (mediaPlayer != null){
                mediaPlayer.stop();
                mBtnMusic.setBackgroundResource(R.drawable.music_close);
                isPlay = false;
            }
        }else {
            PlayMusic();
            mBtnMusic.setBackgroundResource(R.drawable.music_on);
            isPlay = true;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mediaPlayer != null){
            mediaPlayer.stop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (isPlay = true){
            PlayMusic();
        }
    }

    private class Onclick implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.simple:
                    Intent intent1 = new Intent(CheckLevelActivity.this,LLKGameActivity.class);
                    Bundle bundle1 = new Bundle();
                    bundle1.putInt("number",4);
                    bundle1.putString("username",username);
                    intent1.putExtras(bundle1);
                    startActivity(intent1);
                    break;
                case R.id.medium:
                    Intent intent2 = new Intent(CheckLevelActivity.this,LLKGameActivity.class);
                    Bundle bundle2 = new Bundle();
                    bundle2.putInt("number",6);
                    bundle2.putString("username",username);
                    intent2.putExtras(bundle2);
                    startActivity(intent2);
                    break;
                case R.id.difficult:
                    Intent intent3 = new Intent(CheckLevelActivity.this,LLKGameActivity.class);
                    Bundle bundle3 = new Bundle();
                    bundle3.putInt("number",8);
                    bundle3.putString("username",username);
                    intent3.putExtras(bundle3);
                    startActivity(intent3);
                    break;
                case R.id.btn_fragment:
                    Intent intent4 = new Intent(CheckLevelActivity.this, RankingActivity.class);
                    startActivity(intent4);
                    break;
                case R.id.btn_1:
                    Intent intent5 = new Intent(CheckLevelActivity.this, LoadingActivity.class);
                    startActivity(intent5);
                    break;
            }
        }
    }
}
